package email.teste;

import java.text.Collator;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Locale;

public class Localizacao {
	public Localizacao() {
		System.out.println("PAISES DO MUNDO");
		List<Country> listaPaises = this.getPaises();
		for (Country country : listaPaises) {
			System.out.println(country);
		}
		System.out.println("\n\n\n");
		System.out.println("IDIOMAS DO MUNDO");
		List<Language> listaIdiomas = this.getIdiomas();
		for (Language language : listaIdiomas) {
			System.out.println(language);
		}
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		new Localizacao();
	}

	@SuppressWarnings("static-access")
	public List<Language> getIdiomas() {
		Locale local = Locale.getDefault();
		String[] idiomas = local.getISOLanguages();
		List<Language> listaIdiomas = new ArrayList<Language>();

		for (String idioma : idiomas) {
			Locale lc = new Locale(idioma);
			String iso = lc.getISO3Language();
			String code = lc.getLanguage();
			String name = lc.getDisplayLanguage();

			if (!"".equals(iso) && !"".equals(code) && !"".equals(name)) {
				listaIdiomas.add(new Language(iso, code, name));
			}
			Collections.sort(listaIdiomas, new LanguageComparator());
		}
		return listaIdiomas;
	}

	@SuppressWarnings("static-access")
	public List<Country> getPaises() {
		Locale local = Locale.getDefault();
		String idioma = local.getLanguage();
		String[] paises = local.getISOCountries();
		List<Country> listaPaises = new ArrayList<Country>();

		for (String pais : paises) {
			Locale lc = new Locale(idioma, pais);
			String iso = lc.getISO3Country();
			String code = lc.getCountry();
			String name = lc.getDisplayCountry();

			if (!"".equals(iso) && !"".equals(code) && !"".equals(name)) {
				listaPaises.add(new Country(iso, code, name));
			}
		}
		Collections.sort(listaPaises, new CountryComparator());
	    
		return listaPaises;
	}
}

// -------------------------------------------------------------------------------------------------------------------
// -------------------------------------------------------------------------------------------------------------------
class CountryComparator implements Comparator<Country> {
	@SuppressWarnings("rawtypes")
	private Comparator comparator;

	CountryComparator() {
		comparator = Collator.getInstance();
	}

	@SuppressWarnings("unchecked")
	public int compare(Country o1, Country o2) {
		return comparator.compare(o1.name, o2.name);
	}
}

// -------------------------------------------------------------------------------------------------------------------
class Country {
	private String iso;
	private String code;
	public String name;

	Country(String iso, String code, String name) {
		this.iso = iso;
		this.code = code;
		this.name = name;
	}

	public String toString() {
		return iso + " - " + code + " - " + name.toUpperCase();
	}
}

// -------------------------------------------------------------------------------------------------------------------
// -------------------------------------------------------------------------------------------------------------------
class LanguageComparator implements Comparator<Language> {
	@SuppressWarnings("rawtypes")
	private Comparator comparator;

	LanguageComparator() {
		comparator = Collator.getInstance();
	}

	@SuppressWarnings("unchecked")
	public int compare(Language o1, Language o2) {
		return comparator.compare(o1.name, o2.name);
	}
}

// -------------------------------------------------------------------------------------------------------------------
class Language {
	private String iso;
	private String code;
	public String name;

	Language(String iso, String code, String name) {
		this.iso = iso;
		this.code = code;
		this.name = name;
	}

	public String toString() {
		return iso + " - " + code + " - " + name.toUpperCase();
	}
}